#ifndef NMATCHKEYWORD_H
#define NMATCHKEYWORD_H
#include <QtWidgets>
#include "solar/trunk/NSyntaxHignLight.h"

class NMatchKeyWord : public NSyntaxHignLight{

    Q_OBJECT

public:

    NMatchKeyWord(QTextDocument *docuMent) : NSyntaxHignLight(docuMent), mightFlag(false){
        RdoMap_Switch.insert("Backward", false);
        RdoMap_Switch.insert("CaseSensitive", false);
    }

    void findX(QString exprChar){
        bool contrastEnd;
        //# 判断是否新的关键字
        QList<QString> _keyList = exprChar.split(QRegularExpression("\\&\\&|\\-\\>|\\s+\\+\\s+|\\s+\\=\\s+"));
        (historyKeyWord.isEmpty() || mightFlag) ? contrastEnd = true : contrastEnd = (historyKeyWord != _keyList);
        if (contrastEnd) //#如果是新的关键字，则开始匹配光标
        {
            InitializeFromKeyasIndex();
            foreach(QString k, _keyList)
            {
                if (RdoMap_Switch["CaseSensitive"])
                {
                    QRegularExpression findReg(k);
                    cursorList.append(getGlobalMatchingOftextCursor(document()->toPlainText(), findReg, document()));
                }
                else
                {
                    QRegularExpression findReg(skipTolowerFromExpr(k));
                    cursorList.append(getGlobalMatchingOftextCursor(document()->toPlainText().toLower(), findReg, document()));
                }
                diary(QString("key: %1, cursorlist itemcount: %2, casesensitive state: %3").arg(
                                      exprChar,
                                      QString::number(cursorList.count()),
                                      QString::number(RdoMap_Switch["CaseSensitive"])),
                                      Diary::Debug);
            }
            QTextCharFormat SpecialFormat;
            SpecialFormat.setBackground(SpecialFormatColor.first);
            SpecialFormat.setForeground(SpecialFormatColor.second);
            foreach(QString k, _keyList)
            {
                if (contrastEnd)
                {
                    appendHignLight(k,
                                    globalFormatColor.first,
                                    globalFormatColor.second);
                }
            }
            //# 发送索引值和格式数据至光标类，凸显关键字字符
            setSpecialFormat(SpecialFormat);
            mightFlag = false;
        }
        //# 光标数据不为空，则正常累加或递减索引值
        if (!cursorList.isEmpty() &&
            RdoMap_Integer["IndexOf"] >= -1 &&
            RdoMap_Integer["IndexOf"] < cursorList.size())
        {
            if (!RdoMap_Switch["Backward"])
            {
                if (RdoMap_Integer["IndexOf"] + 1 == cursorList.count())
                {
                    RdoMap_Integer["IndexOf"] = 0;
                }
                else
                {
                    RdoMap_Integer["IndexOf"] += 1;
                }
            }
            else
            {
                if (RdoMap_Integer["IndexOf"] <= 0)
                {
                    RdoMap_Integer["IndexOf"] = cursorList.count() - 1;
                }
                else
                {
                    RdoMap_Integer["IndexOf"] -= 1;
                }
            }
            //# 发送焦点信号，使窗口跟随焦点滚动
            emit cutsor_send(cursorList[RdoMap_Integer["IndexOf"]]);
        }
        else
        {
            if (cursorList.isEmpty())
            {
                diary("highlight cursor data is empty", Diary::Debug);
            }
            else
            {
                diary(QString("highlight cursor list index is over range->index %1").arg(
                                      RdoMap_Integer["IndexOf"]), Diary::Debug);
            }
        }

        historyKeyWord = _keyList;
    }

    void setFindBackward(bool Enable){
        RdoMap_Switch["Backward"] = Enable;
    }

    void setCaseSensitive(bool Enable){
        RdoMap_Switch["CaseSensitive"] = Enable;
    }

    void appendSpecialFormat(QColor backColor, QColor foreColor){
        SpecialFormatColor.first = backColor;
        SpecialFormatColor.second = foreColor;
    }

    void appendGlobalFormat(QColor backColor, QColor foreColor){
        globalFormatColor.first = backColor;
        globalFormatColor.second = foreColor;
    }

    void setTemproperyMight(){
        mightFlag = true;
    }

private:

    //# 初始化索引值为1，并删除原关键字
    void InitializeFromKeyasIndex(){
        if (!historyKeyWord.isEmpty())
        {
            foreach(QString k, historyKeyWord) removeHighLightKey(k);
        }
        cursorList.clear();
        RdoMap_Integer["IndexOf"] = -1;
        diary("init index", Diary::Debug)
    }

    //# 全局匹配光标
    QList<QTextCursor> getGlobalMatchingOftextCursor(QString sourceStr, QRegularExpression regexp, QTextDocument *docuMent){
        QPair<QList<int>, QList<int>> cursorListResultPair;
        QList<QTextCursor> cursorList;
        QRegularExpressionMatchIterator Match = regexp.globalMatch(sourceStr);
        while (Match.hasNext())
        {
           QRegularExpressionMatch match = Match.next();
           int capStart = match.capturedStart();
           cursorList.append(docuMent->find(regexp, capStart));
        }
        return cursorList;
    }

private:
    QMap<QString, int> RdoMap_Integer;
    QMap<QString, bool> RdoMap_Switch;
    QPair<QColor, QColor> SpecialFormatColor;
    QPair<QColor, QColor> globalFormatColor;
    QList<QString> historyKeyWord;
    QList<QTextCursor> cursorList;
    QList<QString> _keyList;
    bool mightFlag; //当更新文本框内容时，给与一个临时的全局光标匹配的权限

signals:
    void cutsor_send(QTextCursor);
};
#endif



